<html>
<head><meta charset="utf-8"><title>Testing nested functions in hir_ty · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Testing.20nested.20functions.20in.20hir_ty.html">Testing nested functions in hir_ty</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="228995060"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Testing%20nested%20functions%20in%20hir_ty/near/228995060" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Testing.20nested.20functions.20in.20hir_ty.html#228995060">(Mar 05 2021 at 17:48)</a>:</h4>
<p>It looks like we can't write a test like this in <code>hir_ty</code>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">Tr</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">method</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">u8</span> <span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Tr</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">outer</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">().</span><span class="n">method</span><span class="p">();</span><span class="w"></span>
<span class="w">  </span><span class="c1">//^^^^^^^^^^^ u8</span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">inner</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="p">().</span><span class="n">method</span><span class="p">();</span><span class="w"></span>
<span class="w">      </span><span class="c1">//^^^^^^^^^^^ u8</span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>This causes <a href="https://github.com/rust-analyzer/rust-analyzer/blob/2b55cce49ecb7b8ff5a3b7c917c469929ed643ab/crates/hir_ty/src/tests.rs#L94">this</a> unwrap to fail, because it expects the function to be inside the main module. Is there any other way to do this than to copy all the source-to-def machinery from <code>hir</code> to <code>hir_ty</code>? (<code>hir_def</code> seems more logical, actually)</p>



<a name="228995861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Testing%20nested%20functions%20in%20hir_ty/near/228995861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Testing.20nested.20functions.20in.20hir_ty.html#228995861">(Mar 05 2021 at 17:54)</a>:</h4>
<p>I suppose I could come up with some hack, but at that point using source-to-def seems like the better option</p>



<a name="228997850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Testing%20nested%20functions%20in%20hir_ty/near/228997850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Testing.20nested.20functions.20in.20hir_ty.html#228997850">(Mar 05 2021 at 18:06)</a>:</h4>
<p>Hmm, I could copy <code>module_at_position</code> from <code>hir_def</code>, but that duplicates code too</p>



<a name="228998753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Testing%20nested%20functions%20in%20hir_ty/near/228998753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Testing.20nested.20functions.20in.20hir_ty.html#228998753">(Mar 05 2021 at 18:11)</a>:</h4>
<p>Looks like <code>module_for_file</code> is also duplicated. Maybe we want to expose the <code>hir_def</code> <code>TestDB</code> and reuse it in <code>hir_ty</code>?</p>



<a name="229098624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Testing%20nested%20functions%20in%20hir_ty/near/229098624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Testing.20nested.20functions.20in.20hir_ty.html#229098624">(Mar 06 2021 at 13:37)</a>:</h4>
<p>Yeah.... that's a tough design question. It feels like some part of source2def machinery does really belong to hir, but it is unclear which precise semantics we want there</p>



<a name="229098695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Testing%20nested%20functions%20in%20hir_ty/near/229098695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Testing.20nested.20functions.20in.20hir_ty.html#229098695">(Mar 06 2021 at 13:38)</a>:</h4>
<p>I'll try to think about this today. Basically, what I really want is to keep hir 100% precise, but s2d is somewhat lossy</p>



<a name="229547510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Testing%20nested%20functions%20in%20hir_ty/near/229547510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Testing.20nested.20functions.20in.20hir_ty.html#229547510">(Mar 09 2021 at 20:28)</a>:</h4>
<blockquote>
<p>I'll try to think about this today. </p>
</blockquote>
<p>did happen :(</p>



<a name="229552469"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Testing%20nested%20functions%20in%20hir_ty/near/229552469" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Testing.20nested.20functions.20in.20hir_ty.html#229552469">(Mar 09 2021 at 20:58)</a>:</h4>
<p>For now, I went for <code>check_infer</code> instead, which just prints all types in all (nested) functions</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>